﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace cpe.cpein.SERVER.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GroupsService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select GroupsService.svc or GroupsService.svc.cs at the Solution Explorer and start debugging.
    public class GroupsService : IGroupsService
    {
        /// <summary>
        /// Get All the Group of the database.
        /// </summary>
        /// <param name="searchInDeleted"> Search into the deleted Group too</param>
        /// /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public List<Group> GetAllGroups(bool searchInDeleted, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.Group> Group = new List<DATA.Group>(BUSINESS.BUGroup.GetAllGroup(searchInDeleted));
            List<Group> wsGroup = new List<Group>();
            foreach (DATA.Group tmp in Group)
                wsGroup.Add(Tools.ObjectConverter.GroupConverter(tmp));

            return wsGroup;
        }

        /// <summary>
        /// Get the Group by id.
        /// </summary>
        /// <param name="id"> Id of the Group to get </param>
        /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public Group GetGroupByID(int id, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            if (id > 0)
            {
                DATA.Group Group = BUSINESS.BUGroup.GetGroupByID(id);
                if (Group != null)
                   return Tools.ObjectConverter.GroupConverter(Group);
            }

            return null;
        }

        /// <summary>
        /// Add a Group into the database.
        /// </summary>
        /// <param name="Group">Group to add</param>
        /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public Group AddGroup(Group Group, User current)
        {
            DATA.Group newGroup = BUSINESS.BUGroup.AddGroup(Tools.ObjectConverter.GroupConverter(Group));
            return Tools.ObjectConverter.GroupConverter(newGroup);
        }

        /// <summary>
        /// Remove a specific Group of the database.
        /// </summary>
        /// <param name="Group"> Group to remove </param>
        /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public bool RemoveGroup(Group Group, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;

            if (Group != null)
            {
                return BUSINESS.BUGroup.RemoveGroup(Tools.ObjectConverter.GroupConverter(Group));
            }

            return false;
        }

        /// <summary>
        /// Update the Group into the database.
        /// </summary>
        /// <param name="Group">Group to update</param>
        /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public Group UpdateGroup(Group Group, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            return Tools.ObjectConverter.GroupConverter(BUSINESS.BUGroup.UpdateGroup(Tools.ObjectConverter.GroupConverter(Group)));
        }

        /// <summary>
        /// Function used to get all the GroupTypes.
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<GroupType> GetAllGroupTypes(User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
            {
                return Tools.ObjectConverter.GroupTypeConverter(BUSINESS.BUGroupType.GetAllGroupType(false));
            }
        }
    }
}
